/*
 * httphandleexecutor.cpp
 *
 *  Created on: Apr 26, 2009
 *      Author: kuncai
 */
#include "httphandleexecutor.h"
HttpHandleExecutor::HttpHandleExecutor(){
	GETChain = NULL;
	initGETChain();
}

HttpHandleExecutor::~HttpHandleExecutor(){
	if(GETChain != NULL){
		delete GETChain;
	}
}

HttpHandleExecutor* HttpHandleExecutor::httpHandleExecutor = NULL;

HttpHandleExecutor* HttpHandleExecutor::Create(){
	if(httpHandleExecutor == NULL){
		httpHandleExecutor = new HttpHandleExecutor();
	}
	return httpHandleExecutor;
}

int HttpHandleExecutor::initGETChain(){
	GETChain = new HttpHandleFoundFile();		
	assert(GETChain != NULL);

	HttpHandle* hdLastModified = NULL;
	hdLastModified	= new HttpHandleLastModified();
	assert(hdLastModified != NULL);
	GETChain->SetSuccessor(hdLastModified);

	HttpHandle* hdIfModifiedSince = NULL;
	hdIfModifiedSince = new HttpHandleIfModifiedSince();
	assert(hdIfModifiedSince != NULL);
	hdLastModified->SetSuccessor(hdIfModifiedSince);
	hdLastModified->SetError(hdIfModifiedSince);

	HttpHandle* hdCharset = new HttpHandleCharset();
	assert(hdCharset != NULL);
	hdIfModifiedSince->SetError(hdCharset);

	HttpHandle* hdCoding = new HttpHandleCoding();
	assert(hdCoding != NULL);
	hdCharset->SetSuccessor(hdCoding);
	/*
	HttpHandle* hd200 = NULL;
	hd200 = new HttpHandle200();
	assert(hd200 != NULL);
	hdIfModifiedSince->SetError(hd200);
	*/
}

void HttpHandleExecutor::executor(HttpRequest* httpRequest, HttpResponse* httpResponse){
	string sMethod = httpRequest->GetMethod();
	if(sMethod == "GET"){
		GETChain->HandleRequest(httpRequest, httpResponse);
	}
}
